/*
 * Copyright (c) Jack魏 2023 - 2023, All Rights Reserved.
 */

package cn.jackwei.yiyi.controller.index;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ShearCaptcha;
import cn.hutool.captcha.generator.MathGenerator;
import cn.hutool.core.math.Calculator;
import cn.jackwei.yiyi.exception.BusinessException;
import cn.jackwei.yiyi.pojo.bean.SysUser;
import cn.jackwei.yiyi.pojo.vo.SysUserForgetVO;
import cn.jackwei.yiyi.pojo.vo.SysUserLoginVO;
import cn.jackwei.yiyi.pojo.vo.SysUserVO;
import cn.jackwei.yiyi.service.sys.SysLoginService;
import cn.jackwei.yiyi.service.sys.SysUserService;
import cn.jackwei.yiyi.util.MyCacheUtil;
import cn.jackwei.yiyi.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * 公共接口
 *
 * @author Jack魏
 * @since 2023/11/9 23:00
 */
@Slf4j
@Validated
@Tag(name = "开放接口", description = "开放接口")
@RestController
@RequestMapping("/index")
public class IndexController {
    @Resource
    private SysUserService sysUserService;
    @Resource
    private SysLoginService sysLoginService;

    /**
     * 登录之后并将token传给前台
     *
     * @param request 请求信息
     * @param vo      登录信息
     * @return 登录是否成功
     */
    @Operation(summary = "登录接口", description = "输入账号和密码进行登录")
    @PostMapping("/login")
    public Result<SysUser> login(HttpServletRequest request, @RequestBody @Validated SysUserLoginVO vo) {
        return sysUserService.login(request, vo);
    }

    /**
     * 退出登录
     *
     * @return 是否成功
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        sysLoginService.logout();
        return Result.success("退出成功");
    }

    /**
     * 获取验证码
     *
     * @param key      关键字
     * @param response 响应体
     * @throws IOException io异常
     */
    @Parameter(name = "key", description = "key", required = true)
    @GetMapping("/getCaptcha")
    public void getCaptcha(@NotEmpty(message = "key不可为空") String key,
                           HttpServletResponse response) throws IOException {
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(180, 40, 4, 4);
        // 自定义验证码内容为四则运算方式
        captcha.setGenerator(new MathGenerator());
        // 重新生成code
        captcha.createCode();
        final int code = (int) Calculator.conversion(captcha.getCode());
        log.info("生成的验证码结果是：{}", code);
        captcha.write(response.getOutputStream());

        MyCacheUtil.put(key, code + "", 3);
    }

    /**
     * 注册后台系统用户
     *
     * @param vo 前端注册信息
     * @return 注册用户信息
     */
    @Operation(summary = "注册接口", description = "输入用户信息进行注册")
    @PostMapping("/register")
    public Result<String> register(@RequestBody @Validated SysUserVO vo) throws BusinessException {
        Integer flag = sysUserService.register(vo);
        if (flag <= 0) {
            return Result.fail("注册失败！账号已存在！");
        }
        return Result.successMsg("注册成功，欢迎您：" + vo.getAccount());
    }

    /**
     * 忘记密码
     *
     * @param vo 更改信息
     * @return 是否修改成功
     */
    @Operation(summary = "忘记密码", description = "忘记密码")
    @PostMapping("/forget")
    public Result<String> forget(@RequestBody @Validated SysUserForgetVO vo) {
        return sysUserService.forget(vo);
    }
}
